package edu.spec.chenli.Struts2;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.Cookie;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

import static com.opensymphony.xwork2.Action.SUCCESS;
import static java.util.jar.Pack200.Packer.ERROR;


public class LoginAction_2 implements Action {
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    private String username;
    private String password;

    public String execute()throws  Exception {
        ActionContext ac = ActionContext.getContext();
        Integer counter = (Integer) ac.getApplication().get("counter");
        if (counter == null) {
            counter = 1;
        } else {
            counter = counter + 1;
        }
        ac.getApplication().put("counter", counter);
        ac.getSession().put("user", getUsername());
        if (getUsername().equals("abc") && getPassword().equals("123")) {
            Cookie c = new Cookie("user", getUsername());
            c.setMaxAge(60 * 60);
            ServletActionContext.getResponse().addCookie(c);
            ac.put("tip", "服务器提示：你已经成功第登录");
            return SUCCESS;
        }
        ac.put("tip", "服务器提示：登录失败");
        return ERROR;
    }

    public Object getValue(String key) {
        return null;
    }

    public void putValue(String key, Object value) {

    }

    public void setEnabled(boolean b) {

    }

    public boolean isEnabled() {
        return false;
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {

    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {

    }

    public void actionPerformed(ActionEvent e) {

    }
}
